﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace TM.Model.Mapper
{
    public class ProjectMapper:IMapper<Project>
    {
        #region Implementation of IMapper<Project>

        public Project Map(DataRow dr)
        {
            return new Project()
                       {
                           ID = new Guid(dr["ID"].ToString()),
                           Name = dr["Name"].ToString(),
                           EstimatedStart = (dr["EstimatedStart"] is System.DBNull ? new DateTime?() : DateTime.Parse(dr["EstimatedStart"].ToString())),
                           ActualStart = (dr["ActualStart"] is System.DBNull ? new DateTime?() : DateTime.Parse(dr["ActualStart"].ToString())),
                           Duration = (dr["Duration"] is System.DBNull ? new decimal?() : decimal.Parse(dr["Duration"].ToString())),
                           ProjectOwnerId = (dr["ProjectOwnerId"] is System.DBNull ? new Guid?() : new Guid(dr["ProjectOwnerId"].ToString())),
                           Description = (dr["Description"] is System.DBNull ? string.Empty : dr["Description"].ToString()),
                           CreatedDate = DateTime.Parse(dr["CreatedDate"].ToString())
                       };
        }

        public IList<Project> Map(DataTable tb)
        {
            if(tb == null || tb.Rows.Count==0) return new List<Project>();

            IList<Project> lst = new List<Project>();
            foreach (DataRow row in tb.Rows)
            {
                lst.Add(Map(row));
            }

            return lst;
        }

        #endregion
    }
}
